package com.mall.huohuo.openapi.controller.member.oauth;

import com.mall.huohuo.enums.ResultEnum;
import com.mall.huohuo.exception.BusinessException;
import com.mall.huohuo.openapi.controller.member.request.oauth.OAuthRequest;
import com.mall.huohuo.openapi.controller.member.vo.oauth.OAuthVo;
import com.mall.huohuo.result.Result;
import com.mall.huohuo.service.auth.domain.OAuthResult;
import com.mall.huohuo.service.auth.param.OAuthParam;
import com.mall.huohuo.service.auth.service.OAuthService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zycoding
 * @version AuthController.java, v 0.1 2020-08-07 16:28 zycoding
 */
@RestController
@RequestMapping("/auth")
public class OAuthController {

    @Autowired
    OAuthService authService;

    @PostMapping("/login")
    public Result<OAuthVo> login(@Validated  @RequestBody OAuthRequest authRequest)
    {

        OAuthParam authParam = new OAuthParam();
        BeanUtils.copyProperties(authRequest, authParam);
        OAuthResult oAuthResult = authService.login(authParam);

        if (oAuthResult.getSuccess()) {
            OAuthVo authVo = new OAuthVo();
            BeanUtils.copyProperties(oAuthResult, authVo);
            return Result.success(authVo);
        }
        throw new BusinessException("请检查账号密码。", ResultEnum.TOKEN_LOSE.getCode());
    }

}
